<?php
if (!isset($InputData['list_type'])) exit('无作品列表类型参数');
$InputData['list_type'] = (int)$InputData['list_type'];
if (!isset($myWork)) {
    require_once("{$CfgPara->root}/model/work.m.php");
    $myWork = new work_m($CfgPara->database);
    //20170112 添加 作品列表的三种排序 最新、推荐、热度
    if (isset($InputData['order_type']) && (
            $InputData['order_type'] == work_m::ListSubOrder_New ||
            $InputData['order_type'] == work_m::ListSubOrder_Rec ||
            $InputData['order_type'] == work_m::ListSubOrder_Hot)
    ) {
        $myWork->work_list_sub_order = (int)$InputData['order_type'];
    }
}
/**
 * pc版管理列表名称
 */
$manage_view = '';
if ($InputData['list_type'] == work_m::ListManage) {
    //管理作品页面
    if ($isTablet === TRUE) {
        $manage_view = '_manage';
    }
    if (!isset($_SESSION['user_level']) || !isset($_SESSION['user_id']) || $_SESSION['user_level'] < 10) {
        //权限不够 进入登陆页面
        require_once("{$CfgPara->root}/controller/user_login.c.php");
        goto page_end;
    } else {
        $work_list_id = $_SESSION['user_id'];
        if (!isset($InputData['search_text']))
            $worksCount = $myWork->work_count(work_m::CountMyWork, $work_list_id);
        if (!isset($InputData['page_change'])) {
            //连接搜索栏
            $searchTittle = "管理作品";
            require("{$viewRoot}search_bar{$viewEx}");
        }
    }
} elseif ($InputData['list_type'] == work_m::ListNew) {
    //仅搜索时 会连接到此
    if ($isTablet === TRUE) {
        $manage_view = '_manage';
    }
    $work_list_id = 0;
    $worksCount = 1; //搜索时数量将是查询时确定
} elseif (($InputData['list_type'] == work_m::ListTopicOK ||
        $InputData['list_type'] == work_m::ListTopicOKHot ||
        $InputData['list_type'] == work_m::ListTopicOKRecommend
    ) &&
    isset($InputData['topic_id']) && isset($topicMsg)
) {
    //专题下的作品列表
    $worksCount = $topicMsg->WorksCount;
    $work_list_id = $InputData['topic_id'];
} elseif ($InputData['list_type'] == work_m::ListTopicWait && isset($InputData['topic_id'])) {
    //专题下的待审核作品列表
    if ($isTablet === TRUE) {
        $manage_view = '_manage';
    }
    $work_list_id = $InputData['topic_id'];
    $worksCount = $myWork->work_count(work_m::ListTopicWait, $work_list_id);
    if (!isset($InputData['page_change'])) {
        $tittleText = '待审核作品';
        require("{$viewRoot}tittle_back_bar{$viewEx}");
    }
} elseif ($InputData['list_type'] == work_m::ListMyUp) {
    //我的推荐页面
    if (!isset($InputData['works_user_id'])) {
        if (!isset($_SESSION['user_id'])) {
            //权限不够 进入登陆页面
            require_once("{$CfgPara->root}/controller/user_login.c.php");
            goto page_end;
        }
        $work_list_id = $_SESSION['user_id']; //本人查看我的推荐页面
        if ($isTablet === TRUE) {
            $manage_view = '_manage';
        }
    } else
        $work_list_id = $InputData['works_user_id']; //别人从个人主页中查看此用户的推荐作品
    if (!isset($InputData['search_text']))
        $worksCount = $myWork->work_count(work_m::CountMyUp, $work_list_id);
    if (!isset($InputData['page_change']) && !isset($InputData['works_user_id'])) {
        //连接搜索栏
        $searchTittle = "我推荐的作品";
        require("{$viewRoot}search_bar{$viewEx}");
    }
} elseif ($InputData['list_type'] == work_m::ListMyCollect) {
    //我的收藏页面
    if ($isTablet === TRUE) {
        $manage_view = '_manage';
    }
    if (!isset($_SESSION['user_level']) || !isset($_SESSION['user_id']) || $_SESSION['user_level'] < 10) {
        //权限不够 进入登陆页面
        require_once("{$CfgPara->root}/controller/user_login.c.php");
        goto page_end;
    } else {
        $work_list_id = $_SESSION['user_id'];
        if (!isset($InputData['search_text']))
            $worksCount = $myWork->work_count(work_m::CountMyCollect, $work_list_id);
        if (!isset($InputData['page_change'])) {
            //连接搜索栏
            $searchTittle = "收藏夹的作品";
            require("{$viewRoot}search_bar{$viewEx}");
        }
    }
} elseif ($InputData['list_type'] == work_m::ListUserWorksRelease || $InputData['list_type'] == work_m::ListUserWorksDraft) {
    //个人作品
    if (!isset($InputData['works_user_id'])) goto page_end;
    $work_list_id = $InputData['works_user_id'];
    $count_type = $InputData['list_type'];
    $worksCount = $myWork->work_count($count_type, $work_list_id);
} elseif ($InputData['list_type'] == work_m::ListUserField ||
    $InputData['list_type'] == work_m::ListUserFieldForOrder
) {
    if ($InputData['list_type'] == work_m::ListUserFieldForOrder && $isTablet === TRUE) {
        $manage_view = '_manage';
    }
    if (isset($_SESSION['user_field_work_list'])) {
        $work_list_id = -1;
        //在user_field.c中 已将数据库记录的ids(以逗号分隔)写入对话中
        $worksCount = 1;//本领的数量在查询时再确定
    } elseif (isset($InputData['user_field_id'])) {
        //对话中没有缓存，则从数据库中获取
        $work_list_id = $InputData['user_field_id'];
        $worksCount = 1;//本领的数量在查询时再确定
    }
} elseif ($InputData['list_type'] == work_m::ListUserForField) {
    if ($isTablet === TRUE) {
        $manage_view = '_manage';
    }
    $work_list_id = $_SESSION['user_id'];
    $worksCount = $myWork->work_count(work_m::CountUserWorksRelease, $work_list_id);
} elseif ($InputData['list_type'] == work_m::ListUserForTopic) {
    if (!isset($_SESSION['user_level']) || !isset($_SESSION['user_id']) || $_SESSION['user_level'] < 10) {
        //权限不够 进入登陆页面
        require("{$CfgPara->root}/controller/user_login.c.php");
        goto page_end;
    } else {
        if ($isTablet === TRUE) {
            $manage_view = '_manage';
        }
        $work_list_id = $_SESSION['user_id'];
        $worksCount = $myWork->work_count(work_m::CountUserWorksRelease, $work_list_id);
        $searchPostData = "{topic_id:{$InputData['topic_id']}}";
        if (!isset($InputData['page_change'])) {
            $tittleText = '选择投稿作品';
            require("{$viewRoot}tittle_back_bar{$viewEx}");
        }
    }
}

if (isset($worksCount) || isset($InputData['search_text'])) {
    if (isset($InputData['page_change']) && $InputData['page_change'] > 0) {
        $page_index = $InputData['page_change'];
    } else {
        $page_index = 1;
    }

    //连接作品列表
    if ((isset($worksCount) && is_numeric($worksCount) && $worksCount > 0) || isset($InputData['search_text'])) {
        if (isset($InputData['search_text'])
            && isset($InputData['search_content'])
            && $InputData['search_content'] == 'works_list'
        ) {
            // 如果有搜索内容 则增加搜索文本 且得出总数量
            // 搜索内容的换页js脚本 此函数声明在search.c.php搜索控制器中
            $change_page_func = get_search_page_change_js();
            if ($page_index != 1 &&
                isset($InputData['search_result_count']) &&
                is_numeric($InputData['search_result_count']) &&
                $InputData['search_result_count'] >= 0
            ) {
                //如果页面已经记录过搜索结果总数量
                $workList = $myWork->work_list(
                    $InputData['list_type'],
                    $work_list_id, $page_index,
                    $CfgPara->pagesize, $InputData['search_text']);
                $worksCount = $InputData['search_result_count'];
            } else {
                //如果页面为第一页 或没记录过总数量
                $workList = $myWork->work_list(
                    $InputData['list_type'],
                    $work_list_id, $page_index,
                    $CfgPara->pagesize, $InputData['search_text'], TRUE);
                $worksCount = $myWork->work_list_count;
            }
            //返回的结果用连续两个||符号分隔 结果数量与内容
            echo $worksCount . '||';
        } else {
            $change_page_func = "work_page_change({$InputData['list_type']},[x])";
            $workList = $myWork->work_list($InputData['list_type'], $work_list_id, $page_index, $CfgPara->pagesize);
        }

        if (is_string($workList)) {
            echo($workList);
            goto page_end;
        }
        if ($InputData['list_type'] == work_m::ListUserField ||
            $InputData['list_type'] == work_m::ListUserFieldForOrder
        ) {
            //本领中的作品数量 在查询过程中确定
            $worksCount = $myWork->work_list_count;
        }
        if (isset($InputData['topic_id']) && $InputData['list_type'] == work_m::ListUserForTopic) {
            //如果为从列表中选择作品加入专题 则检测作品列表是否已经加入了当前专题
            $topicAddCheckResult = $myWork->work_add_topic_check($workList, $InputData['topic_id']);
            if (is_string($topicAddCheckResult)) {
                echo($topicAddCheckResult);
                goto page_end;
            }
        }
        if (isset($change_page_func) && $CfgPara->pagesize > 0) {
            //连接分页类 在列表最后加入分页栏
            require_once("{$CfgPara->root}/class/paginate.class.php");
            $myPaginate = new paginate_c();
            $myPaginate->change_func = $change_page_func;
            $paginateMsg = $myPaginate->paginate($page_index, ceil($worksCount / $CfgPara->pagesize));
        }
    }
    if (!isset($InputData['page_change']))
        require("{$viewRoot}works{$manage_view}_list{$viewEx}");
    else
        require("{$viewRoot}works{$manage_view}_list_page{$viewEx}");
}

page_end:;